//
//  YSGBindCardChangePhoneView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/11/19.
//  Copyright © 2020 QDSG. All rights reserved.
//

import UIKit

enum YSGBindCardChangePhoneViewAction {
    case sendSmsCode
    case confirmIdentity
    case applyforModification
}

class YSGBindCardChangePhoneView: UIView, UITextFieldDelegate {
    
    private var viewModel = BindCardViewModel(model: BindCardModel())
    
    var handleClosure: ((YSGBindCardChangePhoneViewAction, String) -> Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        makeUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        makeUI()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.frame = mainWindow.bounds
    }
    
    private func makeUI() {
        alpha = 0.0
        backgroundColor = .clear
    }
    
    @objc private func dismiss() {
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear, animations: {
            self.contentView.transform = CGAffineTransform(scaleX: 0.0001, y: 0.0001)
        }) { _ in
            self.alpha = 0.0
            self.removeFromSuperview()
        }
    }
    
    func show(in view: UIView? = nil, vm: BindCardViewModel) {
        let tempView = view == nil ? mainWindow : view
        tempView?.addSubview(self)
        
        configure(with: vm)
        
        contentView.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
        
        UIView.animate(withDuration: 0.25, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: .curveLinear, animations: {
            self.alpha = 1.0
            self.contentView.transform = CGAffineTransform(scaleX: 1, y: 1)
        })
    }
    
    private func configure(with vm: BindCardViewModel) {
        viewModel = vm
        
        messageLabel.text = "您在医院预留的手机号码\(vm.hosPhoneNoFour)，与您当前手机号不一致，系统将向您预留手机号发送验证码确认身份。"
    }
    
    @IBOutlet private weak var maskbgView: UIView! {
        didSet {
            maskbgView.backgroundColor = UIColor.black.withAlphaComponent(0.7)
            
            let tap = UITapGestureRecognizer(target: self, action: #selector(dismiss))
            maskbgView.addGestureRecognizer(tap)
        }
    }
    
    @IBOutlet private weak var contentView: UIView! {
        didSet {
            contentView.backgroundColor = .white
            contentView.layer.cornerRadius = 16
            contentView.clipsToBounds = true
        }
    }
    
    @IBOutlet private weak var messageLabel: UILabel! {
        didSet {
            messageLabel.textColor = .textDark
            messageLabel.font = .regular_14
            messageLabel.numberOfLines = 0
        }
    }
    
    @IBOutlet private weak var splitLine: UIView!

    @IBOutlet private weak var codeTextField: UITextField! {
        didSet {
            codeTextField.delegate = self
        }
    }
    
    @IBOutlet private weak var sureBtn: UIButton! {
        didSet {
            sureBtn.layer.cornerRadius = sureBtn.height / 2
            sureBtn.clipsToBounds = true
            sureBtn.setTitle("确认", for: .normal)
            sureBtn.setTitleColor(.white, for: .normal)
            sureBtn.setBackgroundImage(UIImage(color: .mainColor), for: .normal)
        }
    }
    
    @IBAction private func getCodeAction(_ sender: CountDownButton) {
        sender.countDown(with: 60)
        handleClosure?(.sendSmsCode, viewModel.phone)
    }
    
    /// 申请修改
    @IBAction private func applyForModificationAction() {
        dismiss()
        handleClosure?(.applyforModification, "")
    }
    
    /// 确定
    @IBAction private func sureAction() {
        if codeTextField.text!.isEmpty {
            HUDTool.show(message: "验证码为空")
            return
        }
        
        dismiss()
        
        handleClosure?(.confirmIdentity, codeTextField.text!)
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        splitLine.backgroundColor = .splLineSelect
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        splitLine.backgroundColor = .splLineNormal
    }
}
